<template>
	<div>
		<el-form :inline="true" size="mini">
			<el-form-item>
				<el-button type="primary" @click="addSalereturn">新增退货单</el-button>
			</el-form-item>
		</el-form>
		
		
		<el-table :data="pageInfo.list" style="100%">
			<el-table-column label="退货编号" prop="id" width="80"></el-table-column>
			<el-table-column label="商品名称" prop="goods.goodsname" width="80"></el-table-column>
			<el-table-column label="客户名称" prop="customer.shortname" width="150"></el-table-column>
			<el-table-column label="单号" prop="orderid" width="150"></el-table-column>
			<el-table-column label="退货日期" prop="returndate" width="160"></el-table-column>
			<el-table-column label="商品数量" prop="goodssum" width="160"></el-table-column>
			<el-table-column label="单价" prop="price" width="150"></el-table-column>
			<el-table-column label="总金额" prop="amount" width="150"></el-table-column>	
		</el-table>
		
		<div>
			<el-pagination
				:total="pageInfo.total"
				:page-count="pageInfo.navigatePages"
				:page-size="pageInfo.pageSize"
				:current-page="pageInfo.pageNum"
				layout="prev,pager,next,total,jumper"
				background
				@current-change="handleItemCurrentChange"
			></el-pagination>
		</div>
		
		<el-dialog :visible.sync="addSalereturns" title="新增退货单" center>
			<el-form :inline="true" align="center" size="mini">
				<el-form-item label="客户名称:">
					<el-select placeholder="客户名称" v-model="type.customer.id">
						<el-option
							v-for="list in customerList"
							:key="list.id"
							:label="list.longname"
							:value="list.id"
						>	
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="商品类别:">
					<el-select placeholder="商品类别" v-model="type.goodstype.id" @change="getGoods()">
						<el-option
							v-for="item in goodsTypeList"
							:key="item.id"
							:label="item.goodstype"
							:value="item.id"
						>	
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="商品名称:">
					<el-select placeholder="商品名称" v-model="type.goods.id" @change="getSum()">
						<el-option
							v-for="item in goodsList"
							:key="item.id"
							:label="item.goodsname"
							:value="item.id"
						>	
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="单号:">
					<el-input v-model="type.orderid" placeholder="单号" ></el-input>
				</el-form-item>	
				<el-form-item label="退货日期:">
					<el-date-picker type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="销售日期" style="width: 150px;" v-model="type.returndate"></el-date-picker>
				</el-form-item>
				<el-form-item label="商品退货数量:">
					<el-input v-model="type.goodssum" placeholder="商品数量" @change="shishi"></el-input>
				</el-form-item>
				<el-form-item label="单价:">
					<el-input v-model="type.price" placeholder="单价" readonly=""></el-input>
				</el-form-item>
				<el-form-item label="总金额:">
					<el-input v-model="type.amount" placeholder="总金额" readonly=""></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="insertType()">保存</el-button>
					<el-button type="warning" @click="quxiao()">取消</el-button>
				</el-form-item>
			</el-form>
		</el-dialog>
		
	</div>
</template>

<script>
	export default{
		data(){
			return{
				keyword:"",
				pageInfo:{
					pageNum:1,
					pageSize:5
				},
				type:{
					customer:{},
					goodstype:{},
					goods:{},
					price:'',
					amount:''
				},
				addSalereturns:false,
				goodsTypeList:[],
				goodsList:[],
				customerList:[],
				inventoryList:[],
				bigType2:{}
			}
		},
		created() {
			this.showData(1,this.pageInfo.pageSize,this.keyword);
		},
		methods:{
			showData(pageNum,pageSize,keyword){
				let url = "salesreturn/getPaged";
				let params = `pageNum=${pageNum}&pageSize=${pageSize}&keyword=${keyword}`;
				axios.post(url,params).then(resp=>{
					this.pageInfo = resp.data.data;
					// console.log(this.pageInfo.list);
				});
			},
			addSalereturn(){
				this.addSalereturns = true;
				
				let url = "salesman/getAllCustomer"
				axios.post(url).then(resp=>{
					this.customerList = resp.data.data;
					// console.log(this.customerList);
				});
				
				url = "salesreturn/getGoodsType";
				axios.post(url).then(resp=>{
					this.goodsTypeList = resp.data.data;
					// console.log(this.goodsTypeList);
				});
			},
			handleItemCurrentChange(pageNum){
				this.showData(pageNum,this.pageInfo.pageSize);
			},
			quxiao(){
				this.addSaleouts = false;
			},
			getGoods(){
				let url = "salesreturn/getGoods";
				axios.post(url,`goodstypeid=${this.type.goodstype.id}`).then(resp=>{
					this.goodsList = resp.data.data;
					// console.log(this.goodsList);
				});
			},
			getSum(){
				let url = "salesreturn/getSum";
				axios.post(url,`goodsid=${this.type.goods.id}`).then(resp=>{
					this.inventoryList = resp.data.data[0];
					this.type.price = this.inventoryList.saleprice;
					// console.log(this.inventoryList);
				})
			},
			quxiao(){
				this.addSalereturns = false;
			},
			insertType(){
				let sum = parseInt(this.inventoryList.currentsum) + parseInt(this.type.goodssum); 
				
				
				this.inventoryList.currentsum = sum;
				
				this.bigType2.salesreturn = this.type;
				this.bigType2.inventory = this.inventoryList;
				console.log(this.bigType2);
				
				let url = "salesreturn/insertUpdate";
				axios.post(url,this.bigType2).then(resp=>{
					if(resp.data.data == 1){
						this.$alert("cg");
					}
				});
			},
			// shishi(){
			// 	this.type.amount = this.type.price*this.type.goodssum;
			// }
			
		},
		computed:{
			shishi(){
				return this.type.amount = this.type.price*this.type.goodssum;
			}
		}

		
	}
</script>

<style>
</style>
